<template>
	<view class="content">
		<view class="background-ys">
			<image src="../../static/xmg.png" mode="aspectFit" />
			<view class="text-title">小 马 哥</view>
			<view class="text-title2">Xiao Ma Ge</view>
			<view class="text-body">物 流 货 运 平 台</view>
		</view>
		<view class="body">
			<view class="body1">
				<radio-group name="radio" @change="onRadioChange">
					<label>
						<radio :value="value" />
					</label>
				</radio-group>
				<view class="body1-content">我已阅读并同意 <text style="color: #4daff5; cursor: pointer;"
						@click="serviceAgreement">服务协议 </text> 和
					<text style="color: #4daff5; cursor: pointer;"
						@click="privacyPolicy">隐私政策</text>,允许申请获取手机号登录权限,未注册用户将自动为您注册小马哥快运账户
				</view>
			</view>
			<!--都是复制-->
			<!-- <view class="btn" @click="login">快捷登录</view> -->
			<!-- #ifdef APP -->
			<view class="" style="margin-top: 200rpx;width: 100%;display: flex;justify-content: center;height: 200rpx;">
				<image src="/static/images/login_icon/weixin.png" mode="" style="width: 100rpx;height: 100rpx;" @click="getWeiXinLogin"></image>
			</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<button v-if="value == -1" class="btn" @click="ifValue">快捷登录</button>
			<button v-else class="btn" open-type="getPhoneNumber" @getphonenumber="getUserPhone">快捷登录</button>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	import {
		baseUrl,
		appId,
		// AppS
	} from '@/util/config.js'
	import {
		loginAPI,
		addPublicize,
		getTel,
		ifFirstLogin,
	} from '@/api/user.js'
	import {
		wxLogin_app
	} from '@/api/index.js'

	export default {
		data() {
			return {
				title: 'Hello',
				value: -1,
				phoneNumber: "",
				receiveOpenId: ''
			}
		},
		onLoad(option) {
			if (option) {
				this.receiveOpenId = option.scene
			}
			console.log(option, this.receiveOpenId,'登录获取分享者openid');
			let token = uni.getStorageSync('X-Access-Token') ? true : false
			if (token) {
				uni.switchTab({
					url: '/pages/expressPages/expressPages',
				})
			}
		},
		methods: {
			serviceAgreement() {
				uni.navigateTo({
					url: "/pages/index/serviceAgreement"
				})
			},
			privacyPolicy() {
				uni.navigateTo({
					url: "/pages/index/privacyPolicy"
				})
			},
			//微信快捷登录   小程序版
			ifValue() {
				uni.showToast({
					title: '请勾选协议',
					duration: 2000,
					icon: "none"
				});
			},
			getUserPhone(e) {
				uni.showLoading({
					title: '登录中'
				})
				let that = this
				if (that.value == -1) {
					uni.showToast({
						title: '请勾选协议',
						duration: 2000,
						icon: "none"
					});	
					return false;
				}
				console.log('获取用户手机号',e)
				let detail = e.detail
				if (detail.errMsg === "getPhoneNumber:ok") {
					let code = detail.code
					// console.log(code, "手机号code");
					getTel({
						code: e.detail.code
					}, 'get').then(res => {
						console.log('手机号',res);
						that.phoneNumber = res.phone_info.phoneNumber
						if (res.errcode == 40029) {
							uni.showToast({
								title: '手机号获取失败',
								icon: 'error'
							});
						} else {
							uni.login({
								success: (loginRes) => {
									let params = {
										code: loginRes.code,
										shareUserId: 0,
										userInfo: {}
									}
									params.userInfo.phone = res.phone_info.phoneNumber
									that.loginData(params)
								},
							})
						}
					})
				 } else {
					uni.showToast({
						title: '用户拒绝授权',
						duration: 2000,
						icon: "none"
					});
					uni.switchTab({
						url: "/pages/expressPages/expressPages"
					})
				}
			},
			//微信登录 app版
			getWeiXinLogin(){
				const that = this
				if (that.value == -1) {
					uni.showToast({
						title: '请勾选协议',
						duration: 2000,
						icon: "none"
					});	
					return false;
				}
				// console.log(123)
				uni.showLoading({
					title: '登录中'
				})
				uni.login({
					provider: 'weixin',
					onlyAuthorize:true,
					success: function (res) {
						console.log(res); // 获取登录结果
						wxLogin_app({code:res.code}, 'post')
							.then((ress) => {
								// uni.showToast({
								// 	title: "登录成功",
								// 	icon: "none"
								// })
								console.log(ress)
								if(ress.errno == 800){
									uni.setStorage({
										key: 'wx_token',
										data: ress.errmsg,
									})
									uni.showToast({
										title: "登录成功",
										icon: "none"
									})
									let settime = setTimeout(()=>{
										uni.navigateTo({
											url: "/pages/index/addphone?OpenId=" + ress.errmsg 
										})
										clearTimeout(settime)
									},1000)
								}else if(ress.errno == 0){
									// ifFirstLogin({openId:ress.data.userInfo.userId}, 'get')
									// .then((res) => {
									// 	if(res){
									// 		uni.setStorageSync('isfirstlogin', res);
									// 	}
									// })
									uni.setStorageSync("user",ress.data.userInfo)
									uni.setStorageSync("X-Access-Token",ress.data.token)
									// console.log(uni.getStorageSync('user'))
									uni.showToast({
										title: "登录成功",
										icon: "none"
									})
									let settime = setTimeout(()=>{
										uni.switchTab({
											url: "/pages/expressPages/expressPages"
										})
										clearTimeout(settime)
									},1000)
								}
							})
							.catch(err=>{
								console.log(err)
							})
						// that.wx_login(res.code)
					},
					fail:function (err) {
						console.log(err)
					}
				})
			},

			
			
			loginData(params) {
				let that = this
				console.log(params);
				loginAPI(params, 'post').then(res => {
					
					uni.hideLoading();
					uni.setStorage({
						key: 'X-Access-Token',
						data: res.data.token,
						success() {
							// ifFirstLogin({openId:res.data.userInfo.userId}, 'get')
							// .then((res) => {
							// 	if(res){
							// 		uni.setStorageSync('isfirstlogin', res);	
							// 	}
							// })
							
							console.log(that.receiveOpenId,res.data.userInfo,'登录');
							if (that.receiveOpenId) {
								let param = {
									openId: res.data.userInfo.openid,  
									promoterOpenId: that.receiveOpenId
								}
								addPublicize(param, 'post').then(res => {
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: "none"
									});
								})
							}
							uni.setStorage({
								key: 'user',
								data: res.data.userInfo,
							})
							uni.switchTab({
								url: "/pages/expressPages/expressPages"
							})
						},
					})
				})
			},
			onRadioChange(e) {
				this.value = 1;
				console.log(this.value)
				// console.log("============", e.detail.value)
			}

		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100%;
	}

	.background-ys {
		width: 100%;
		height: 800rpx;
		//background: rgb(192, 266, 73);
		background: rgb(204, 255, 1);
		//background-image: url('../../static/bjtp.png');
		position: relative;
		// border: 1rpx solid red;

		image {
			width: 200rpx;
			height: 200rpx;
			margin-top: 50rpx;
			margin-left: 270rpx;
		}

		.text-title {
			font-size: 100rpx;
			font-weight: bold;
			color: rgb(215, 94, 81);
			margin-left: 200rpx;
		}

		.text-title2 {
			font-size: 60rpx;
			font-weight: bold;
			color: rgb(84, 109, 165);
			margin-left: 210rpx;
		}

		.text-body {
			font-size: 60rpx;
			padding-top: 10rpx;
			padding-left: 160rpx;
			color: rgb(208, 92, 79);
			// border: 1rpx solid red;
			// font-weight: bold;
		}
	}

	.body {
		width: 100%;
		position: absolute;
		top: 580rpx;
		background: white;
		border-radius: 30rpx;

		.body1 {
			// border: 1rpx solid red;
			margin-top: 100rpx;
			display: flex;

			radio {
				// border: 1rpx solid red;
				padding-left: 30rpx;
				padding-top: 18rpx;
			}

			.body1-content {
				padding-left: 20rpx;
				// border: 1rpx solid red;
				color: rgb(209, 209, 209);
				font-size: 30rpx;
			}

		}

		.btn {
			height: 100rpx;
			// border: 1rpx solid red;
			margin: 70rpx auto;
			background: rgb(204, 255, 1);
			text-align: center;
			line-height: 100rpx;
			color: rgb(215, 94, 81);
			width: 700rpx;
			border-radius: 30rpx;
			font-weight: bold;

		}
	}
</style>